package com.itheima.info.manage.sysytem.dao;

import com.itheima.info.manage.sysytem.domain.Student;

public class StudentDao {
    //创建学生数组
    //定义变量index为-1，假设数组已经全部存满，没有null的元素
    int index=-1;
    //遍历数组取出每一个元素，判断是否是null
    private static Student[]stus=new Student[5];
    public boolean addStudent(Student stu) {
        for (int i = 0; i <stus.length ; i++) {
            Student student = stus[i];
            if (student == null) {
                index = i;
                //如果为null，让index变量记录当前索引的位置，并使用break结束遍历循环
                break;
            }
        }
        //返回是否添加成功的boolean类型状态
            if (index==-1){
                //装满了
                return false;
            }else {
                stus[index]=stu;
                //没有装满正常添加，返回ture
                return true;
            }
    }

    public Student[] findallStudent() {
        return stus;
    }

    public void deleteStudent(String sid) {
        int index=getIndex(sid);
        stus[index]=null;


    }

    public int getIndex(String sid) {
        int index=-1;
        for (int i = 0; i <stus.length ; i++) {
            Student stu=stus[i];
            if (stu!=null&&stus[i].getSid().equals(sid)){
                index=i;
                break;
            }
        }
        return index;
    }

    public void updateStuent(String sid,Student newstu) {
        int index=getIndex(sid);
        stus[index]=newstu;
    }
}
